package net.xdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.xdclass.request.CartItemRequest;
import net.xdclass.service.CartService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Classname CartController
 * @Description TODO
 * @Date 2022/5/11 15:41
 * @Created by sunhao
 */
@Api(tags = "购物车控制器")
@RestController
@RequestMapping("/api/cart/v1")
public class CartController{

  @Autowired
  private CartService cartService;


  @ApiOperation(value = "添加购物车")
  @PostMapping(value = "add")
  private JsonData addToCart(@RequestBody CartItemRequest cartItemRequest){
    cartService.addToCart(cartItemRequest);
    return JsonData.buildSuccess();
  }

  @ApiOperation(value = "查询我的购物车")
  @GetMapping(value = "list")
  public JsonData  list(@RequestParam(value = "latestPrise",defaultValue = "false") Boolean latestPrise){
    return JsonData.buildSuccess(cartService.list(latestPrise));
  }

  @ApiOperation(value = "清空我的购物车")
  @GetMapping(value = "clear")
  public JsonData clear(){
    cartService.clear();
    return JsonData.buildSuccess();
  }

  @ApiOperation(value = "删除购物车中某一项商品")
  @DeleteMapping("/delete/{product_id}")
  private JsonData delete(@PathVariable("product_id") String productId){
    cartService.delete(productId);
    return JsonData.buildSuccess();
  }


  @ApiOperation(value = "修改购物车信息")
  @PutMapping(value = "/update")
  public JsonData update(@RequestBody CartItemRequest cartItemRequest){
    cartService.update(cartItemRequest);
    return JsonData.buildSuccess();
  }

  @PostMapping("/confirm_order_cart_item")
  public JsonData confirmOrderCartItem(@RequestBody  List<Long> productIdList){
    return JsonData.buildSuccess(cartService.confirmOrderCartItem(productIdList));
  }

}
